<template>
  <div class="container mt-5">
    <div class="row">
      <div class="col-12">
        <div class="card">
          <div class="card-header">
            <h3 class="mb-0"><i class="fas fa-building mr-2"></i>添加古建筑</h3>
          </div>
          <div class="card-body">
            <form @submit.prevent="submitForm">
              <div class="form-group">
                <label for="name">名称</label>
                <input type="text" class="form-control" id="name" v-model="form.name" required>
              </div>
              <div class="form-group">
                <label for="type">类型</label>
                <select class="form-control" id="type" v-model="form.type" required>
                  <option value="">请选择类型</option>
                  <option value="寺庙">寺庙</option>
                  <option value="祠堂">祠堂</option>
                  <option value="古宅">古宅</option>
                  <option value="塔楼">塔楼</option>
                </select>
              </div>
              <div class="form-group">
                <label for="location">地点</label>
                <input type="text" class="form-control" id="location" v-model="form.location" required>
              </div>
              <div class="form-group">
                <label for="year">建造年代</label>
                <input type="text" class="form-control" id="year" v-model="form.year" required>
              </div>
              <div class="form-group">
                <label for="protectionStatus">保护状态</label>
                <select class="form-control" id="protectionStatus" v-model="form.protectionStatus" required>
                  <option value="">请选择保护状态</option>
                  <option value="完好">完好</option>
                  <option value="部分损坏">部分损坏</option>
                  <option value="严重损坏">严重损坏</option>
                  <option value="已修复">已修复</option>
                </select>
              </div>
              <div class="form-group">
                <label for="description">描述</label>
                <textarea class="form-control" id="description" v-model="form.description" rows="4"></textarea>
              </div>
              <div class="form-group">
                <label for="image">图片</label>
                <input type="file" class="form-control-file" id="image" @change="handleImageUpload">
              </div>
              <div class="mt-4">
                <button type="submit" class="btn btn-primary mr-2">
                  <i class="fas fa-save mr-1"></i>保存
                </button>
                <router-link to="/architecture" class="btn btn-secondary">
                  <i class="fas fa-arrow-left mr-1"></i>返回
                </router-link>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ArchitectureAdd',
  data() {
    return {
      form: {
        name: '',
        type: '',
        location: '',
        year: '',
        protectionStatus: '',
        description: '',
        image: null
      }
    }
  },
  methods: {
    handleImageUpload(event) {
      this.form.image = event.target.files[0]
    },
    async submitForm() {
      try {
        const formData = new FormData()
        Object.keys(this.form).forEach(key => {
          formData.append(key, this.form[key])
        })

        const response = await fetch('/api/architecture/add', {
          method: 'POST',
          body: formData
        })

        if (response.ok) {
          this.$router.push('/architecture')
        } else {
          alert('保存失败，请重试')
        }
      } catch (error) {
        console.error('保存失败:', error)
        alert('保存失败，请重试')
      }
    }
  }
}
</script>

<style scoped>
/* 卡片样式 */
.card {
  border: none;
  border-radius: 15px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  background: linear-gradient(145deg, #ffffff 0%, #f8f9fa 100%);
}

/* 卡片头部样式 */
.card-header {
  background-color: #2c3e50;
  color: white;
  border-bottom: none;
  padding: 1.5rem 1.5rem 1rem;
  border-radius: 15px 15px 0 0;
}

.card-header h3 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 600;
  display: flex;
  align-items: center;
}

.card-header i {
  color: #3498db;
  margin-right: 0.5rem;
  font-size: 1.2rem;
}

/* 表单样式 */
.form-group {
  margin-bottom: 1.75rem;
}

.form-group label {
  font-weight: 500;
  margin-bottom: 0.75rem;
  font-size: 0.95rem;
  color: #333;
}

.form-control {
  border: 1px solid #e9ecef;
  border-radius: 8px;
  padding: 0.85rem 1rem;
  font-size: 0.95rem;
  transition: all 0.3s ease;
}

.form-control:focus {
  border-color: #3498db;
  box-shadow: 0 0 0 0.2rem rgba(52, 152, 219, 0.25);
}

/* 选择框样式 */
select.form-control {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%236c757d' d='M6 8L2 4l1.5-1.5L6 5.25 9.5 1.75 11 3l-5 5z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.75rem center;
  background-size: 12px 12px;
  padding-right: 2.5rem;
}

/* 文本区域样式 */
textarea.form-control {
  min-height: 150px;
  resize: vertical;
  padding: 1rem;
}

/* 上传图片样式 */
.form-control-file {
  padding: 0.75rem;
  border: 1px dashed #e9ecef;
  border-radius: 8px;
  background: #f8f9fa;
  color: #6c757d;
  cursor: pointer;
  transition: all 0.3s ease;
}

.form-control-file:hover {
  background: #f1f3f5;
  border-color: #3498db;
}

/* 按钮样式 */
.btn {
  padding: 0.85rem 1.75rem;
  border-radius: 8px;
  font-weight: 500;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.btn-primary {
  background-color: #3498db;
  border: none;
}

.btn-primary:hover {
  background-color: #2980b9;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(52, 152, 219, 0.2);
}

.btn-secondary {
  background-color: #6c757d;
  border: none;
}

.btn-secondary:hover {
  background-color: #5a6268;
  transform: translateY(-1px);
}

/* 间距类 */
.mr-2 {
  margin-right: 0.75rem;
}

.mr-1 {
  margin-right: 0.5rem;
}

/* 建筑类型图标 */
.architecture-type-icon {
  font-size: 1.2rem;
  margin-right: 0.5rem;
}

.architecture-type-temple {
  color: #f1c40f;
}

.architecture-type-cemetery {
  color: #2ecc71;
}

.architecture-type-residence {
  color: #3498db;
}

.architecture-type-tower {
  color: #e74c3c;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .card-header h3 {
    font-size: 1.25rem;
  }
  
  .form-control {
    padding: 0.7rem 0.85rem;
    font-size: 0.9rem;
  }
  
  .btn {
    padding: 0.7rem 1.4rem;
    font-size: 0.9rem;
  }
  
  .form-group {
    margin-bottom: 1.5rem;
  }
}
</style>
